#!/usr/bin/env python
#encoding=utf8
"""
@描述：验证我们的对照表跟抓取来的数据比对，看看数据效果
       1.匹配上的：IT-品管、技术支持及其它	其他运维/测试/技术支持	15597	15597
	   2.没有匹配上的：首席财务官CFO
@日期：2015年01月29日 星期四

@作者：hongxingfan
"""

import sys
reload(sys)
sys.setdefaultencoding("utf8")
from  DBUtils import MySQLDB
from DBArgs import *
import re

# 类区域，封装职能名称+ID
class ThirdInfo:
	def __init__(self,name,id):
		self.name = name
		self.id = id

# 程序入口
if __name__ == "__main__":

	selectMap = """
		SELECT siteName,NAME,j.thirdName,o.thirdId FROM jobs.otherSiteToKanZhun o
		LEFT JOIN jobs.jobclass_tag j
		ON o.thirdId = j.thirdId
		ORDER BY siteName ASC
		"""
	# 连接配置
	db = MySQLDB(DB251.host,DB251.user,DB251.passwd,DB251.port,DB251.charset)
	# 封装成Map<string,Map<string,string>>结构
	keyValue = {}
	keyValue_1 = {} # 职能名-->我们自己的第三级职能标签
	# 执行sql,装载到字典中
	key = None
	param = []
	resultMap = db.select(selectMap,param)
	jobClassMap = dict() # otherSiteToKanZhun中别的公司职能标签名
	for (siteName,name,thirdName,thirdId) in resultMap:
		if key != siteName and key != None:
			keyValue[key] = keyValue_1
			keyValue_1 = {}
		if(name in keyValue_1):
			keyValue_1[name].append(ThirdInfo(thirdName,thirdId))
		else:
			keyValue_1[name] = [ThirdInfo(thirdName,thirdId)]
		key = siteName
		#if(siteName == "job51"):
		#	ll.append(name)
		if(key in jobClassMap):
			jobClassMap[key].add(name)
		else:
			jobClassMap[key] = set(name)
	keyValue[key] = keyValue_1
	# 遍历jobs数据表，找出与keyValue中相匹配的内容
	selectJobs = "SELECT id,provider,jobclass FROM jobs.jobs WHERE id<%s AND _update_at>='2015-01-01' ORDER BY id DESC LIMIT 0,5000"
	param = [sys.maxint]
	kv = {} # 匹配上的职能
	total = 0
	while(True):
		# (id,provider,jobclass)
		result = db.select(selectJobs, param)
		index = 0
		for(id,provider,jobclass) in result:
			if(id < param[0]):
				param[0] = id
			index += 1
			total += 1
			jobclass = jobclass.replace("\"","").replace("[","").replace("]","")
			classes = re.split(",",jobclass)
			provider = provider.lower()
			if(provider not in keyValue or provider not in jobClassMap):
				continue
			jobClassSet = jobClassMap.get(provider)
			for c in classes:
				if c in jobClassSet:
					jobClassSet.remove(c)
				key = ""
				if(c == None or c == ""):
					continue;
				thirdInfoList = keyValue.get(provider).get(c)
				if(thirdInfoList is None):
					key = provider + "\t" + c + "\t" + ""
					if(key in kv):
						kv.get(key)[0] += 1
						kv.get(key)[1] += 0
					else:
						kv[key] = [1,0]
					continue
				for th in thirdInfoList:
					key = provider + "\t" + c + "\t" + th.name
					if(key in kv):
						kv.get(key)[0] += 1
						kv.get(key)[1] += 1
					else:
						kv[key] = [1,1]
		print("Index is: %d" %(index))
		if(index < 40):
			break
	print("总数条数是：%d" %(total))
	# 匹配上的职能
	for (k,v) in kv.items():
		print("%s\t%d\t%d" %(k,v[0],v[1]))
	print("-----------------------------")
	# 没有匹配上的职能
	for (k,v) in jobClassMap.items():
		for vv in v:
			print("%s\t%s" %(k,vv))
	db.close()
